package com.java.demo.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @auther zhangwj
 * @date 2020/9/22 3:40 下午
 */
public class Demo2 {
    // 待路由的Ip列表，Key代表Ip，Value代表该Ip的权重
    public static HashMap<String, Integer> serverWeightMap = new HashMap<>();

    static {
        serverWeightMap.put("192.168.1.100", 1);
        serverWeightMap.put("192.168.1.101", 1);
        // 权重为4
        serverWeightMap.put("192.168.1.102", 4);
        serverWeightMap.put("192.168.1.103", 1);
        serverWeightMap.put("192.168.1.104", 1);
        // 权重为3
        serverWeightMap.put("192.168.1.105", 3);
        serverWeightMap.put("192.168.1.106", 1);
        // 权重为2
        serverWeightMap.put("192.168.1.107", 2);
        serverWeightMap.put("192.168.1.108", 1);
        serverWeightMap.put("192.168.1.109", 1);
        serverWeightMap.put("192.168.1.110", 1);
    }

    public static void main(String[] args) {
        Set<String> keySet = serverWeightMap.keySet();
        List<String> keyList = new ArrayList<>(keySet.size());
        keyList.addAll(keySet);
        int serverListSize = keyList.size();
        for (String key : keyList) {
            System.out.println(key.hashCode() % serverListSize);
        }
    }
}
